﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Polaris.Domain.Interfaces.WebSite;
using Polaris.Domain.Model.DtoWebSite.Article.Response;
using Polaris.Domain.Model.DtoWebSite.Notes.Request;
using Polaris.Domain.Model.Entity.Base;
using Polaris.Domain.Model.Enum;
using System.Security.Cryptography;
using System.Text;

namespace Polaris.WebApi.Controllers.WebSite
{
    /// <summary>
    /// 网址文章控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WebArticleController : BaseController
    {
        private readonly IWebArticle _webArticle;

        public WebArticleController(IWebArticle webArticle)
        {
            _webArticle = webArticle;
        }

        /// <summary>
        /// 获取最新的闲书
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetNewNotes(string? id)
        {
            var fingerprint = GenerateClientFingerprint(HttpContext);
            var result = await _webArticle.GetNewNotes(string.IsNullOrEmpty(id) ? null : Guid.Parse(id), fingerprint);
            return SUCCESS(result);
        }

        /// <summary>
        /// 闲书浏览量
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> BrowseNotes(string id)
        {
            var fingerprint = GenerateClientFingerprint(HttpContext);
            var result = await _webArticle.BrowseNotes(Guid.Parse(id), fingerprint);
            return SUCCESS(result);
        }

        /// <summary>
        /// 文章浏览量
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> BrowseArticle(string id)
        {
            var fingerprint = GenerateClientFingerprint(HttpContext);
            var result = await _webArticle.BrowseArticle(Guid.Parse(id), fingerprint);
            return SUCCESS(result);
        }

        /// <summary>
        /// 获取所有文章列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetArticleList([FromQuery] Pagination pageInfo)
        {
            var result = await _webArticle.GetArticleList(pageInfo);
            return SUCCESS(result);
        }

        /// <summary>
        /// 根据id和类型获取文章或闲书内容
        /// </summary>
        /// <param name="id"></param>
        /// <param name="articleType"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetArticleContentById(string id, ArticleType articleType)
        {
            var result = await _webArticle.GetArticleContentById(id, articleType);
            return SUCCESS(result);
        }

        /// <summary>
        /// 根据id获取文章消息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetArticleInfoById(string id)
        {
            var fingerprint = GenerateClientFingerprint(HttpContext);
            var result = await _webArticle.GetArticleInfoById(id, fingerprint);
            return SUCCESS(result);
        }

        /// <summary>
        /// 获取推荐文章
        /// </summary>
        /// <param name="type">notes/articles</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetRecommendArticle(string type)
        {
            var result = await _webArticle.GetRecommendArticle(type);
            return SUCCESS(result);
        }

        /// <summary>
        /// 根据类型获取文章列表
        /// </summary>
        /// <param name="pageInfo"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetArticleListByType([FromQuery] Pagination pageInfo, ArticleType type)
        {
            var result = await _webArticle.GetArticleListByType(pageInfo, type);
            return SUCCESS(result);
        }

        /// <summary>
        /// 闲书点赞
        /// </summary>
        /// <param name="notesId"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> LikeNotes([FromQuery] string notesId)
        {
            var fingerprint = GenerateClientFingerprint(HttpContext);
            var result = await _webArticle.LikeNotes(notesId, fingerprint);
            return SUCCESS(result);
        }

        /// <summary>
        /// 文章点赞
        /// </summary>
        /// <param name="articleId"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> LikeArticle([FromQuery] string articleId)
        {
            var fingerprint = GenerateClientFingerprint(HttpContext);
            var result = await _webArticle.LikeArticle(articleId, fingerprint);
            return SUCCESS(result);
        }

        /// <summary>
        /// 生成客户端指纹
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private string GenerateClientFingerprint(HttpContext context)
        {
            // 获取真实IP（考虑代理情况）
            var ip = context.Request.Headers["X-Forwarded-For"].FirstOrDefault()
                     ?? context.Connection.RemoteIpAddress?.ToString();

            // 获取浏览器特征
            var userAgent = context.Request.Headers.UserAgent.ToString();

            // 生成混合指纹
            var rawData = $"{ip}|{userAgent}";
            return Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(rawData)));
        }
    }
}
